package com.sinothk.widgets.noticeView.marqueeListView.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinothk.widgets.noticeView.R;
import com.sinothk.widgets.noticeView.marqueeListView.MarqueeListView;
import com.sinothk.widgets.noticeView.marqueeListView.bean.MarqueeListEntity;

import java.util.ArrayList;

public class MarqueeListViewAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<MarqueeListEntity> listData;
    private MarqueeListView.ParaVo paraVo;

    public MarqueeListViewAdapter(Context context, ArrayList<MarqueeListEntity> listData) {
        mContext = context;
        this.listData = listData;
    }

    public MarqueeListViewAdapter(Context context, ArrayList<MarqueeListEntity> listData, MarqueeListView.ParaVo paraVo) {
        mContext = context;
        this.listData = listData;
        this.paraVo = paraVo;
    }

    @Override
    public int getCount() {
        return this.listData == null ? 0 : this.listData.size();
    }

    @Override
    public MarqueeListEntity getItem(int position) {
        return this.listData == null ? null : this.listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.marquee_list_view_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MarqueeListEntity entity = this.listData.get(position);

        holder.itemNameTv.setText(entity.getName());

        if (!TextUtils.isEmpty(entity.getDate())) {
            holder.itemTimeTv.setText(entity.getDate());
            holder.itemTimeTv.setVisibility(View.VISIBLE);
        } else {
            holder.itemTimeTv.setVisibility(View.GONE);
        }

        if (entity.getIconRes() != 0) {
            holder.itemIconIv.setImageResource(entity.getIconRes());
            holder.itemIconIv.setVisibility(View.VISIBLE);
        } else {
            holder.itemIconIv.setVisibility(View.GONE);
        }

        if (this.paraVo != null) {

            if (this.paraVo.getTextColor() != 0) {
                holder.itemNameTv.setTextColor(this.paraVo.getTextColor());
                holder.itemTimeTv.setTextColor(this.paraVo.getTextColor());
            }

            if (this.paraVo.getTextSize() != 0) {
                holder.itemNameTv.setTextSize(this.paraVo.getTextSize());
                holder.itemTimeTv.setTextSize(this.paraVo.getTextSize());
            }

            // 内容左右间距
            if (this.paraVo.getMarginStartEnd() != 0) {
                LinearLayout.LayoutParams itemNameTvLp = (LinearLayout.LayoutParams) holder.itemNameTv.getLayoutParams();
                itemNameTvLp.setMargins(this.paraVo.getMarginStartEnd(), 0, this.paraVo.getMarginStartEnd(), 0);
                holder.itemNameTv.setLayoutParams(itemNameTvLp);

//                RelativeLayout.LayoutParams itemTimeTvLp = (RelativeLayout.LayoutParams) holder.itemTimeTv.getLayoutParams();
//                itemTimeTvLp.setMargins(this.paraVo.getMarginStartEnd(), 0, 0, 0);
//                holder.itemTimeTv.setLayoutParams(itemTimeTvLp);
            }

            // 内容上下间距
            if (this.paraVo.getIconSize() != 0) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                layoutParams.width = this.paraVo.getIconSize();
                layoutParams.height = this.paraVo.getIconSize();
                holder.itemIconIv.setLayoutParams(layoutParams);
            }

            // 内容左右间距，内容上下间距
            if (this.paraVo.getItemPaddingStartEnd() != 0 && this.paraVo.getItemPaddingTopBottom() != 0) {
                holder.rootView.setPadding(this.paraVo.getItemPaddingStartEnd(), this.paraVo.getItemPaddingTopBottom(), this.paraVo.getItemPaddingStartEnd(), this.paraVo.getItemPaddingTopBottom());
            }
        }

        return convertView;
    }

    static class ViewHolder {
        RelativeLayout rootView;
        TextView itemNameTv, itemTimeTv;
        ImageView itemIconIv;

        ViewHolder(View view) {
            rootView = view.findViewById(R.id.rootView);
            itemIconIv = view.findViewById(R.id.itemIconIv);
            itemNameTv = view.findViewById(R.id.itemNameTv);
            itemTimeTv = view.findViewById(R.id.itemTimeTv);
        }
    }
}
